//
//  VideoViewController.m
//  Four_Fit
//
//  Created by xalo on 16/7/12.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "VideoViewController.h"
#import "VideoHeaderView.h"
#import "VideoModel.h"
#import "VideoCell.h"

#import "CourseViewController.h"
#import "TrainViewController.h"

#define kVideoURL @"http://bea.sportq.com/SFitWeb/sfit/getFind?form.device=&form.mySex=0&form.phoneType=1&form.sex=0&form.sign=BDCEE12B8B31662E7459743224327E1E&form.userId=93118&form.version=1.1.5"

@interface VideoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,VideoHeaderViewDelegate>
{
    CGRect headerViewFrame;
}
@property(nonatomic,strong)NSMutableDictionary *recommendDic; //推荐视图信息数组
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *dataSource; //数据源
@property(nonatomic,strong)VideoHeaderView *headerView;
@end

@implementation VideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //导航栏字体颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    [self loadData];
    //collectionView
    [self.view addSubview:self.collectionView];
    [self.collectionView registerNib:[UINib nibWithNibName:@"VideoCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"Video_Cell"];
    //headerView
    [self.collectionView registerNib:[UINib nibWithNibName:@"VideoHeaderView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//获取数据
- (void)loadData{
    //设置缓存路径
    [QZRequestManager sharedManager].filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"FitPage.db"];
    [[QZRequestManager sharedManager] fetchDataWithUrl:kVideoURL parameters:nil method:@"GET" needToCaches:YES fetchData:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        self.recommendDic = [dic[@"entRet"][@"lstCarousel"] firstObject];
        for (NSDictionary *dic1 in dic[@"entRet"][@"lstCurriculum"]) {
            VideoModel *model = [[VideoModel alloc] init];
            [model setValuesForKeysWithDictionary:dic1];
            [self.dataSource addObject:model];
        }
    } updateUI:^{
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, kHeight / 3.0 + 20, kWidth, 30)];
        label.text = @"   课程分类";
        label.font = [UIFont systemFontOfSize:15];
        [self.collectionView addSubview:label];
        [self.collectionView reloadData];
    }];
}
#pragma mark 属性懒加载
- (NSDictionary *)recommendDic{
    if (!_recommendDic) {
        _recommendDic = [[NSMutableDictionary alloc] init];
    }
    return _recommendDic;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
-(UICollectionView *)collectionView{
    if (!_collectionView) {
        //设置Layout
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((kWidth - 1) / 2.0, (kWidth - 1) / 2.0);
        layout.minimumInteritemSpacing = 1.0;
        layout.minimumLineSpacing = 1.0;
        layout.sectionInset = UIEdgeInsetsMake(30, 0, 0, 0);
        //设置头大小
        layout.headerReferenceSize = CGSizeMake(kWidth, kHeight / 3.0 + 20);
        //collectionView
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 49) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        
    }
    return _collectionView;
}
#pragma mark  collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    VideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Video_Cell" forIndexPath:indexPath];
    [cell setCellWithModel:self.dataSource[indexPath.item]];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        self.headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        //设置照片按比例拉伸
        self.headerView.carouselImage.clipsToBounds = YES;
        self.headerView.carouselImage.contentMode = UIViewContentModeScaleAspectFill;
        headerViewFrame = self.headerView.frame;
        [self.headerView.carouselImage sd_setImageWithURL:[NSURL URLWithString:self.recommendDic[@"carouselImageUrl"]]];
        self.headerView.planTitle.text = self.recommendDic[@"planTitle"];
        self.headerView.carouselSummary.text = self.recommendDic[@"carouselSummary"];
        self.headerView.delegate = self;
#warning 本周热门 好像有问题
        self.headerView.carouselTagName.text = self.recommendDic[@"carouselTagName"];
        self.headerView.timeAndOther.text = [NSString stringWithFormat:@"%@分钟  %@千卡  初级",self.recommendDic[@"trainDuration"] ? self.recommendDic[@"trainDuration"] : @"0",self.recommendDic[@"calorie"] ? self.recommendDic[@"calorie"] : @"0"];
        view = self.headerView;
    }
    return view;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CourseViewController *courseVC = [[CourseViewController alloc] init];
    //隐藏下个视图控制器的tabBar
    courseVC.hidesBottomBarWhenPushed = YES;
    courseVC.classifyId = [self.dataSource[indexPath.row] classifyId];
    courseVC.myTitle = [self.dataSource[indexPath.row] curriculumName];
    [self.navigationController pushViewController:courseVC animated:YES];
}
//跳转
- (void)goAway{
    TrainViewController *trainVC = [[TrainViewController alloc] initWithNibName:@"TrainViewController" bundle:[NSBundle mainBundle]];
    
    trainVC.hidesBottomBarWhenPushed = YES;
    trainVC.planId = self.recommendDic[@"planId"];
    trainVC.myTitle = self.recommendDic[@"planTitle"];
    [self.navigationController pushViewController:trainVC animated:YES];

}

//头下拉变大
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y < 0) {
        self.headerView.frame = CGRectMake(headerViewFrame.origin.x,scrollView.contentOffset.y, headerViewFrame.size.width, headerViewFrame.size.height - scrollView.contentOffset.y);
    }
    
}
@end
